<?php
function encodenumber($number='')
{
    $n = explode(',',$number);
    if(count($n)>1)
    {
        if($n[0]=='')
            $n[0]='0';
	
        $number = $n[0].'.'.$n[1];
    }
    return $number;
}

function decodenumber($number='')
{
    $n = explode('.',$number);
    if(count($n)>1)
    {
        if($n[0]==0)
            $n[0]='';
        
	$number =','.$n[1];
    }
    return $number;
}
function encode($string){
	$CI =& get_instance();
    $CI->load->library(array('encrypt'));
	$CI->load->helper(array('string'));
	if(!$CI->session->userdata('key')){
		$random = random_string('alnum', 16);
		$CI->session->set_userdata('key',$random);
	}
	//$key = sha1(md5($CI->session->userdata('key')));
	$key = $CI->session->userdata('key');
	
	$code = str_replace("/","$key",$CI->encrypt->encode($string,$key));
	return $code;
}

function decode($string){
	$CI =& get_instance();
    $CI->load->library(array('encrypt'));
	
	//$key = sha1(md5($CI->session->userdata('key')));
	$key = $CI->session->userdata('key');
	
	$string = str_replace("$key","/",$string);
	
	return $CI->encrypt->decode($string,$key);
}

function dateToIndo($date){
	$BulanIndo = array("Januari", "Februari", "Maret",
					   "April", "Mei", "Juni",
					   "Juli", "Agustus", "September",
					   "Oktober", "November", "Desember");

	$tahun = substr($date, 0, 4);
	$bulan = substr($date, 5, 2);
	$tgl   = substr($date, 8, 2);
	
	$result = $tgl . " " . $BulanIndo[(int)$bulan-1] . " ". $tahun;		
	return($result);
}

function get_bulan($n=0){
	$bulan=array(
		'01'=>'Januari',
		'02'=>'Pebruari',
		'03'=>'Maret',
		'04'=>'April',
		'05'=>'Mei',
		'06'=>'Juni',
		'07'=>'Juli',
		'08'=>'Agustus',
		'09'=>'September',
		'10'=>'Oktober',
		'11'=>'November',
		'12'=>'Desember'
	);
	
	if($n==0)
		return $bulan;
	else
		return $bulan[$n];
}

function get_day($n=0){
	$day = array(
		'1' => 'Senin',
		'2' => 'Selasa',
		'3' => 'Rabu',
		'4' => 'Kamis',
		'5' => "Jum'at",
		'6' => 'Sabtu',
		'7' => 'Minggu'
	);
	
	if($n==0)
		return $day;
	else
		return $day[$n];
}

function fetch_record($path)
{
	// $file = fopen($path, "r"); 
	// if (!$file)
	// {
		// exit("Problem occured");
	// } 
	// $data = '';
	// while (!feof($file))
	// {
		// $data .= fgets($file, 1024);
	// }
	$ch = curl_init(); 
	curl_setopt($ch, CURLOPT_URL, $path); 
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
	curl_setopt($ch, CURLOPT_TIMEOUT, 20);	
	$data = curl_exec($ch); 
	$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
	curl_close($ch);
	
	return $data;
}

function checkUrl($value)
{
	$value = trim($value);
	if (get_magic_quotes_gpc()) 
	{
		$value = stripslashes($value);
	}
	$value = strtr($value, array_flip(get_html_translation_table(HTML_ENTITIES)));
	$value = strip_tags($value);
	$value = htmlspecialchars($value);
	return $value;
}

function getTags($html, $tagName, $attrName, $attrValue ){
	$dom = new DOMDocument('1.0', 'utf-8');
	@$dom->loadHTML($html);
    $domxpath = new DOMXPath($dom);
    $newDom = new DOMDocument;
    $newDom->formatOutput = true;

    $filtered = $domxpath->query("//$tagName" . '[@' . $attrName . "='$attrValue']");
    // $filtered =  $domxpath->query('//div[@class="className"]');
    // '//' when you don't know 'absolute' path

    // since above returns DomNodeList Object
    // I use following routine to convert it to string(html); copied it from someone's post in this site. Thank you.
    $i = 0;
    while( $myItem = $filtered->item($i++) ){
        $node = $newDom->importNode( $myItem, true );    // import node
        $newDom->appendChild($node);                    // append node
    }
    $html = $newDom->saveHTML();
    return $html;
}

function removeTags($html, $idname){
	$dom = new DOMDocument('1.0', 'utf-8');
	@$dom->loadHTML($html);
	$element = $dom->getElementById($idname);
	$element->parentNode->removeChild($element);
	$html = $dom->saveHTML();
    return $html;
}

function terbilang( $num ,$dec=4){
    $stext = array(
        "Nol",
        "Satu",
        "Dua",
        "Tiga",
        "Empat",
        "Lima",
        "Enam",
        "Tujuh",
        "Delapan",
        "Sembilan",
        "Sepuluh",
        "Sebelas"
    );
    $say  = array(
        "Ribu",
        "Juta",
        "Milyar",
        "Triliun",
        "Biliun", // remember limitation of float
        "--apaan---" ///setelah biliun namanya apa?
    );
    $w = "";

    if ($num <0 ) {
        $w  = "Minus ";
        //make positive
        $num *= -1;
    }

    $snum = number_format($num,$dec,",",".") or die($snum);
    $strnum =  explode(".",substr($snum,0,strrpos($snum,",")));
    //parse decimalnya
    $koma = substr($snum,strrpos($snum,",")+1);

    $isone = substr($num,0,1)  ==1;
    if (count($strnum)==1) {
        $num = $strnum[0];
        switch (strlen($num)) {
            case 1:
            case 2:
                if (!isset($stext[$strnum[0]])){
                    if($num<19){
                        $w .=$stext[substr($num,1)]." Belas";
                    }else{
                        $w .= $stext[substr($num,0,1)]." Puluh ".
                            (intval(substr($num,1))==0 ? "" : $stext[substr($num,1)]);
                    }
                }else{
                    $w .= $stext[$strnum[0]];
                }
                break;
            case 3:
                $w .=  ($isone ? "Seratus" : terbilang(substr($num,0,1)) .
                    " Ratus").
                    " ".(intval(substr($num,1))==0 ? "" : terbilang(substr($num,1)));
                break;
            case 4:
                $w .=  ($isone ? "Seribu" : terbilang(substr($num,0,1)) .
                    " Ribu").
                    " ".(intval(substr($num,1))==0 ? "" : terbilang(substr($num,1)));
                break;
            default:
                break;
        }
    }else{
        $text = $say[count($strnum)-2];
        $w = ($isone && strlen($strnum[0])==1 && count($strnum) <3? "Se".strtolower($text) : terbilang($strnum[0]).' '.$text);
        array_shift($strnum);
        $i =count($strnum)-2;
        foreach ($strnum as $k=>$v) {
            if (intval($v)) {
                $w.= ' '.terbilang($v).' '.($i >=0 ? $say[$i] : "");
            }
            $i--;
        }
    }
    $w = trim($w);
    if ($dec = intval($koma)) {
        $w .= " Koma ". terbilang($koma);
    }
    return trim($w);
}